package customer.ejb.eaos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Hotel;
import model.MeanOfTransportation;
import common.Filter;
import common.IPredicate;
import dtos.HotelDTO;
import dtos.MeanOfTransportationDTO;

@Stateless
public class MeanOfTransportationManagerEAO implements MeanOfTransportationManager{

	@PersistenceContext
    private EntityManager em;
	
	private String departurePlace;
	private String arrivalPlace;
	
	public MeanOfTransportationManagerEAO(){
		
	}
	
	@Override
	public Collection<MeanOfTransportationDTO> getTransportations(String departurePlace, String arrivalPlace) {
		this.departurePlace = departurePlace;
		this.arrivalPlace = arrivalPlace;
		

		@SuppressWarnings("unchecked")
		List<MeanOfTransportation> qres = em.createQuery("SELECT x FROM MeanOfTransportation x").getResultList();
		Collection<MeanOfTransportation> fres = Filter.filter(qres, isOK);
		Collection<MeanOfTransportationDTO> res = new ArrayList<MeanOfTransportationDTO>(fres.size());
		
		
		for(MeanOfTransportation t : fres)
		{
			res.add(t.getDTO());
		}
		
		return res;
		
	}
	
	IPredicate<MeanOfTransportation> isOK = new IPredicate<MeanOfTransportation>() {
		public boolean apply(MeanOfTransportation t) {
	        return t.isFromTo(departurePlace,arrivalPlace);
	    }

	};
	
}
